<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
    <el-breadcrumb-item :to="{ path: '/' }" style="font-size: 19px;">
      <i class="el-icon-s-home"></i> 个人中心
    </el-breadcrumb-item>
    <el-breadcrumb-item style="font-size: 17px">商品代买</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>
  <div style="margin-top: 20px;margin-bottom: 20px">
    <el-input
        placeholder="用户名"
        prefix-icon="el-icon-search"
        v-model="input2" style="width: 500px;">
    </el-input>
    <el-button style="margin-left: 20px"><i class="el-icon-search">查询</i></el-button>
    <br>
    <el-button style="margin-top: 20px"><i class="el-icon-delete">删除</i></el-button>
  </div>
  <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55">

    </el-table-column>
    <el-table-column
        prop="id"
        label="用户id"
        align="center"
        width="80">
    </el-table-column>
    <el-table-column
        prop="username"
        label="用户姓名"
        width="100" align="center">
    </el-table-column>
    <el-table-column
        prop="address"
        label="用户地址">
    </el-table-column>
    <el-table-column
        header-align="center"
        prop="phone"
        label="手机号">
    </el-table-column>
    <el-table-column
    prop="purchaseInfo"
    label="购买信息" width="100">
    </el-table-column>
    <el-table-column
        prop="auditStatus"
        label="审核状态" width="80">
      <template slot-scope="scope">
        <span v-if="scope.row.auditStatus==1">审核通过</span>
        <span v-if="scope.row.auditStatus==0">审核失败</span>
      </template>
    </el-table-column>
    <el-table-column
        prop="audit"
        label="审核" align="center" width="100">
      <el-button icon="el-icon-edit" circle size="mini" ></el-button>
    </el-table-column>
    <el-table-column
        prop="gmtCreated"
        label="申请时间">
    </el-table-column>
    <el-table-column
        label="操作" align="center">
      <template slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" circle size="mini"
                   @click="openEditDialog(scope.row)"></el-button>
        <el-button type="danger" icon="el-icon-delete" circle size="mini"
                   @click="openDeleteConfirm(scope.row)"></el-button>
      </template>
<!--      <el-button icon="el-icon-edit" circle size="mini" >配送</el-button>
      <el-button icon="el-icon-edit" circle size="mini" >修改</el-button>
      <el-button icon="el-icon-edit" circle size="mini" >删除</el-button>-->
    </el-table-column>
  </el-table>
  <div style="margin-top: 20px">
    <el-button @click="toggleSelection()">取消选择</el-button>
  </div>
  <el-dialog title="修改审核信息" :visible.sync="dialogFormVisible">
    <el-form :model="editForm">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="editForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="审核状态" :label-width="formLabelWidth">
        <el-input v-model="editForm.auditStatus" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitEditForm()">修 改</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: '120px',
      leveArr:{
        1:"审核通过",
        0:"审核失败"
      },
      tableData: [],
      multipleSelection: [],
      input2:'',
      editForm:{username:'',auditStatus:''}
    }
  },

  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    submitEditForm() {
      let url = 'http://localhost:8090/commoditys/' + this.editForm.id + '/update'
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios.post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = false;
          this.$message({
            message: '修改相册信息成功！',
            type: 'success'
          });
          this.loadAlbumList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAlbumList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openEditDialog(commodity) {
      let url = 'http://localhost:8080/commoditys/' + commodity.id;
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state == 10000) {
          this.editForm = responseBody.data;
          this.dialogFormVisible = true;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          });
        }
      });
    },
    openDeleteConfirm(album) {
      let message = '此操作将永久删除【' + album.name + '】相册，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(album);
      }).catch(() => {
      });
    },
    handleDelete(album) {
      let url = 'http://localhost:9080/albums/' + album.id + "/delete";
      console.log('url = ' + url);

      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.loadAlbumList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadAlbumList() {
      let url = 'http://localhost:8080/commoditys/list';
      console.log('url = ' + url);

      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 10000) {
          this.tableData = responseBody.data;
        }
      });
    },
  },
  mounted() {
    this.loadAlbumList();
  }
}
</script>
<style>

</style>